package com.his.modules.sysjob.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.modules.business.entity.CaSignData;
import com.his.modules.business.service.CaSignDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

@Component("caSynSjcJob")
@Slf4j
public class CaSynSjcJob {
    @Autowired
    private CaSignDataService caSignDataService;
    /**
     * CA同步Body和时间戳
     * @return
     */
    public void caSynSjcJob() {
        log.info("CA获取时间戳定时任务开始执行=================");
        QueryWrapper<CaSignData> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("QMSJC");

        List<CaSignData> caSignDataList = caSignDataService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(caSignDataList)) {
            Integer succNum = 0;
            log.info("CA获取时间戳任务获取到未处理的业务数据条数：{}=================", caSignDataList.size());
            for(CaSignData ca : caSignDataList) {
                String fileName = "C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\20240329-缺少的时间戳数据\\" + ca.getCaId() + ".txt";
                try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        log.info("读取文件{}的行内容：{}", fileName, line);
                        ca.setQmsjc(line);
                        boolean lb;
                        lb = caSignDataService.updateById(ca);
                        if (lb) succNum ++;
                        log.info("CA_ID为{}的数据更新结果为{}", ca.getCaId(), lb);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                log.info("共更新{}条！", succNum);
            }
        }
    }
}